package com.my.net;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;

/**
 * 客户端请求处理器
 */
public class HttpClientHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
    private final ChannelHandlerContext ctx;

    public HttpClientHandler(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpResponse response) throws Exception {
        // 透传
        ctx.writeAndFlush(response.retain());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}